/**
 *  多进程实践，这里是在windows系统下
 */


#include <iostream>
#include <windows.h>
#include <stdio.h>

// 守护进程服务
SERVICE_STATUS serviceStatus;
SERVICE_STATUS_HANDLE serviceStatusHandle;

VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
VOID WINAPI ServiceCtrlHandler(DWORD fdwControl);

int main(int argc, char **argv) 
{
   
    {   // 设置进程优先级
        // 获取当前进程的句柄
        HANDLE hProcess = GetCurrentProcess();

        // 设置进程优先级为高
        if (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS)) {
            std::cout << "Process priority has been set to high." << std::endl;
        } else {
            // 获取错误代码
            DWORD errorCode = GetLastError();
            std::cerr << "Failed to set process priority. Error code: " << errorCode << std::endl;
            return 1;
        }
        
        // 这里可以添加其他需要执行的任务代码
    }

    {
        // 创建进程
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        ZeroMemory(&si, sizeof(si));
        si.cb = sizeof(si);
        ZeroMemory(&pi, sizeof(pi));
    
        if (!CreateProcess(
            NULL,
            TEXT("processEx.exe"), // 调用可执行程序
            NULL,
            NULL,
            FALSE,
            0,
            NULL,
            NULL,
            &si,
            &pi))
        {
            printf("CreateProcess failed: %d\n", GetLastError());
            return 1;
        }else{
            printf("CreateProcess succeed: %d\n", pi.dwProcessId);
        }
        // 等待子进程结束
        // WaitForSingleObject(pi.hProcess, INFINITE);

        CloseHandle(pi.hProcess);
        CloseHandle(pi.hThread);
    }

    {
        // 简单服务
        SERVICE_TABLE_ENTRY serviceTable[] = {
            {TEXT("MyService"), ServiceMain},
            { NULL, NULL }
        };

        if (!StartServiceCtrlDispatcher(serviceTable)) {
            printf("Start Service CtrlDispatcher failed: %d\n", GetLastError());
            return -1;
        }
    }    
    return 0;
}    

VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
 {
    serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    serviceStatus.dwCurrentState = SERVICE_START_PENDING;
    serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    serviceStatus.dwWin32ExitCode = 0;
    serviceStatus.dwServiceSpecificExitCode = 0;
    serviceStatus.dwCheckPoint = 0;
    serviceStatus.dwWaitHint = 0;

    serviceStatusHandle = RegisterServiceCtrlHandler(TEXT("MyService"), ServiceCtrlHandler);
    if (serviceStatusHandle == NULL) {
        printf("ServiceMain Register Service CtrlHandler failed!\n");
        return;
    }

    // 标记服务已启动
    serviceStatus.dwCurrentState = SERVICE_RUNNING;
    SetServiceStatus(serviceStatusHandle, &serviceStatus);

    // 服务主体，例如记录系统日志
    while (serviceStatus.dwCurrentState == SERVICE_RUNNING) {
        // 模拟日志记录
        FILE *fp = fopen("D:\\Logs\\ServiceLog.txt", "a");
        if (fp != NULL) {
            fprintf(fp, "Service is running\n");
            fclose(fp);
        }
        else {
            printf("file open failed!\n");
        }
        Sleep(10000);
    }
}

VOID WINAPI ServiceCtrlHandler(DWORD fdwControl) {
    switch (fdwControl) {
        case SERVICE_CONTROL_STOP:
            serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
            SetServiceStatus(serviceStatusHandle, &serviceStatus);

            // 执行清理操作
            serviceStatus.dwCurrentState = SERVICE_STOPPED;
            SetServiceStatus(serviceStatusHandle, &serviceStatus);
            break;
        default:
            break;
    }
}

